package com.jservice.framework.freemarker.method;

import com.jfinal.i18n.I18n;
import com.jfinal.i18n.Res;
import com.jservice.framework.dto.Setting;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 模板方法 - 多语言
 * 
 * @author cwledit
 * @version 1.0
 */
public class MessageMethod implements TemplateMethodModelEx {

	@SuppressWarnings("rawtypes")
	public Object exec(List arguments) throws TemplateModelException {
		Res resZh = I18n.use("language.message", Setting.getConfig("local"));
		if (arguments != null && !arguments.isEmpty() && arguments.get(0) != null && StringUtils.isNotEmpty(arguments.get(0).toString())) {
			String message = null;
			String code = arguments.get(0).toString();
			if (arguments.size() > 1) {
				Object[] args = arguments.subList(1, arguments.size()).toArray();
				message = resZh.format(code, args);
			} else {
				message = resZh.get(code);
			}
			return new SimpleScalar(message);
		}
		return null;
	}

	public static void main(String[] args) {
		Res resZh = I18n.use("language.message","zh_CN");
		System.out.println(resZh.get("admin.validate.url"));
	}

}